package com.ruoyi.common.utils.aliyun;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.event.ProgressListener;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.ruoyi.common.config.Global;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 *  阿里云 oss 文件上传工具类
 * @author thinker
 */
public class FileUploadOssUtils {

    /**
     * aliyun : keyId && keySecret
     */
    private static String accessKeyId = Global.getAliyunAccessKeyId();
    private static String accessKeySecret = Global.getAliyunAccessKeySecret();

    /**
     * aliyun oss : endpoint && bucketName
     */
    private static String endpoint = Global.getOssEndPoint();
    private static String bucketName = Global.getOssBucketName();


    /**
     *  检查bucketName 是否存在，不存在，立即创建
     * @param oss
     */
    private static void checkBucketExist(OSS oss){
        if (!oss.doesBucketExist(bucketName)) {
            oss.createBucket(bucketName);
        }
    }

    /**
     * 上传文件流到OSS
     * @param inputStream 文件流
     * @param path 存储的文件目录
     * @param fileName 文件名
     * @return 返回上传的路径
     */
    public static String uploadInputStreamToOss(InputStream inputStream, String path, String fileName, ProgressListener progressListener) {
        return uploadInputStreamToOss(inputStream, path, fileName, progressListener,false);
    }
    /**
     * 上传文件到OSS
     * @param file 文件
     * @param path 存储的文件目录
     * @param fileName 文件名
     * @return 返回上传的路径
     */
    public static String uploadFileToOss(File file,String path,String fileName, ProgressListener progressListener) {

        // 上传文件流。
        try {
            InputStream inputStream = new FileInputStream(file);
            return uploadInputStreamToOss(inputStream,path,fileName,progressListener);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 上传视频到OSS
     * @param file 文件
     * @param path 存储的文件目录
     * @param fileName 文件名
     * @return 返回上传的路径
     */
    public static String uploadVideoToOss(File file,String path,String fileName, ProgressListener progressListener) {
        // 上传文件流。
        try {
            InputStream inputStream = new FileInputStream(file);
            return uploadInputStreamToOss(inputStream,path,fileName,progressListener,true);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 上传文件流到OSS
     * @param inputStream 文件流
     * @param path 存储的文件目录
     * @param fileName 文件名
     * @return 返回上传的路径
     */
    private static String uploadInputStreamToOss(InputStream inputStream, String path, String fileName, ProgressListener progressListener,boolean async) {
        //创建对象
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        //检查bucket 是否存在
        checkBucketExist(ossClient);
        // 上传文件流。
        path += "/" + fileName;
        //上传
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, path, inputStream);
        ObjectMetadata meta = new ObjectMetadata();
        meta.setContentType("");
        putObjectRequest.setMetadata(meta);
        if (progressListener != null){
            putObjectRequest.withProgressListener(progressListener);
        }
        if (async){
            new Thread(()->{
                ossClient.putObject(putObjectRequest);
                // 关闭OSSClient。
                ossClient.shutdown();
            }).start();
        }else{
            ossClient.putObject(putObjectRequest);
            // 关闭OSSClient。
            ossClient.shutdown();
        }
        String urlStr = endpoint.replace("https://", "https://" + bucketName+ ".");
        return urlStr+"/"+path;
    }


    /*public static void main(String[] args) {
        String s = uploadFileToOss(new File("E:\\abc.xls"),"aa/bb","abcd.xls");
        System.out.println(s);
    }*/
}
